package com.minshenglife.zanzan.pub.utils;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;
import java.util.TimeZone;

/**
 * ���ܣ�ʱ�䴦�?����
 * @author yubaojian0616@163.com
 *
 * mobile enterprise application platform
 * Version 0.1
 */
public class DateUtil {

	public static final long SECOND = 1000L;
	public static final long MINUTE = 60000L;
	public static final long HOUR = 3600000L;
	public static final long DAY = 86400000L;
	public static final long WEEK = 604800000L;

	/**
	 * ��������ʱ��
	 * @return
	 */
	public static Date getYestoday() {
		return truncate(getDateAgo(1), Calendar.DATE);
	}

	/**
	 * ���ǰdays������ʱ��
	 * @param days
	 * @return
	 */
	public static Date getDateAgo(int days) {
		return getDateAgo(new Date(), days);
	}

	/**
	 * ���ĳʱ��֮ǰdays���ʱ��
	 * @param date
	 * @param days
	 * @return
	 */
	public static Date getDateAgo(Date date, int days) {
		Calendar c = new GregorianCalendar();
		c.setTime(date);
		c.add(Calendar.DATE, -days);
		return c.getTime();
	}

	/**
	 * ���days����ʱ��
	 * @param days
	 * @return
	 */
	public static Date getDateAfter(int days) {
		return getDateAgo(new Date(), -days);
	}

	/**
	 * ���ĳ��ʱ��֮��days���ʱ��
	 * @param date
	 * @param days
	 * @return
	 */
	public static Date getDateAfter(Date date, int days) {
		return getDateAgo(date, -days);
	}

	/**
	 * ��ñ��ܵ�һ��
	 * @return
	 */
	public static Date getFirstDayOfThisWeek() {

		Calendar c = new GregorianCalendar();
		c.set(Calendar.DAY_OF_WEEK, 2);
		return truncate(c.getTime(), Calendar.DATE);
	}

	/**
	 * ��ñ������һ��
	 * @return
	 */
	public static Date getLastDayOfThisWeek() {

		Calendar c = new GregorianCalendar();
		c.set(Calendar.DAY_OF_WEEK, 7);
		c.add(Calendar.DATE, 1);
		return truncate(c.getTime(), Calendar.DATE);
	}

	/**
	 * ��ñ��µ�һ��
	 * @return
	 */
	public static Date getFirstDayOfThisMonth() {

		Calendar c = new GregorianCalendar();
		c.set(Calendar.DAY_OF_MONTH, 1);
		return truncate(c.getTime(), Calendar.DATE);
	}

	/**
	 * ��ñ������һ��
	 * @return
	 */
	public static Date getLastDayOfThisMonth() {

		Calendar c = new GregorianCalendar();
		c.set(Calendar.DAY_OF_MONTH, 1);
		c.add(Calendar.DATE, -1);
		c.add(Calendar.MONTH, 1);
		return truncate(c.getTime(), Calendar.DATE);
	}

	/**
	 * ������ܵ����һ��
	 * @return
	 */
	public static Date getLastDayOfLastWeek() {
		Calendar c = new GregorianCalendar();
		c.set(Calendar.DAY_OF_WEEK, 1);
		return truncate(c.getTime(), Calendar.DATE);
	}

	/**
	 * ������ܵĵ�һ��
	 * @return
	 */
	public static Date getFirstDayOfLastWeek() {
		Calendar c = new GregorianCalendar();
		c.set(Calendar.DAY_OF_WEEK, 1);
		c.add(Calendar.DATE, -6);
		return truncate(c.getTime(), Calendar.DATE);
	}

	/**
	 * ����ϸ��µ����һ��
	 * @return
	 */
	public static Date getLastDayOfLastMonth() {
		Calendar c = new GregorianCalendar();
		c.set(Calendar.DAY_OF_MONTH, 1);
		c.add(Calendar.DATE, -1);
		return truncate(c.getTime(), Calendar.DATE);
	}

	/**
	 * ����ϸ��µ�һ��
	 * @return
	 */
	public static Date getFirstDayOfLastMonth() {
		Calendar c = new GregorianCalendar();
		c.add(Calendar.MONTH, -1);
		c.set(Calendar.DAY_OF_MONTH, 1);
		return truncate(c.getTime(), Calendar.DATE);
	}

	/**
	 * �鿴���������Ƿ���ͬһ��
	 * @param d1
	 * @param d2
	 * @return
	 */
	public static boolean isSameDate(Date d1, Date d2) {
		return org.apache.commons.lang.time.DateUtils.isSameDay(d1, d2);
	}

	/**
	 * �鿴d1�Ƿ���d2֮��
	 * @param d1
	 * @param d2
	 * @return
	 */
	public static boolean afterDate(Date d1, Date d2) {
		d1 = org.apache.commons.lang.time.DateUtils.truncate(d1, Calendar.DATE);
		d2 = org.apache.commons.lang.time.DateUtils.truncate(d2, Calendar.DATE);
		return d1.after(d2);
	}

	/**
	 * �鿴d1�Ƿ���d2֮ǰ
	 * @param d1
	 * @param d2
	 * @return
	 */
	public static boolean beforeDate(Date d1, Date d2) {
		d1 = org.apache.commons.lang.time.DateUtils.truncate(d1, Calendar.DATE);
		d2 = org.apache.commons.lang.time.DateUtils.truncate(d2, Calendar.DATE);
		return d1.before(d2);
	}

	/**
	 * ����������֮��
	 * @param d
	 * @param from
	 * @param to
	 * @return
	 */
	public static boolean isBetweenDate(Date d, Date from, Date to) {
		return (DateUtil.afterDate(d, from) && DateUtil.beforeDate(d, to))
				|| (isSameDate(d, from) || isSameDate(d, to));
	}

	/**
	 * ���ĳ�����ڵ���ֵ
	 * @param date
	 * @return
	 */
	public static Date getDate(Date date) {
		date = (date == null ? new Date() : date);
		return truncate(date, Calendar.DATE);
	}

	/**
	 * �ض�����
	 * @param d1
	 * @param i java.util.Calendar.DATE
	 * @return
	 */
	public static Date truncate(Date d1, int i) {
		return org.apache.commons.lang.time.DateUtils.truncate(d1, i);
	}

	/**
	 * ��ʽ������
	 * @param date
	 * @param pattern
	 * @return
	 */
	public static String format(Date date, String pattern) {
		SimpleDateFormat df = new SimpleDateFormat(pattern, Locale.CHINESE);
		return df.format(date);
	}

	/**
	 * ����ʱ���ַ�
	 * @param dateString
	 * @param format
	 * @return
	 */
	public static Date parse(String dateString, String format) {
		return parse(dateString, format, Locale.CHINESE, TimeZone.getDefault());
	}

	/**
	 * ʱ���ַ�ת�ɺ���
	 * @param dateString
	 * @param format
	 * @return
	 */
	public static Long dateStrToMillis(String dateString, String format) {
		java.util.Date date = DateUtil.parse(dateString, format);
		return date.getTime();
	}

	/**
	 * ����ת��ʱ���ַ�
	 * @param millis
	 * @param format
	 * @return
	 */
	public static String millisToDateStr(Long millis, String format) {
		java.util.Date date = DateUtil.getDateByCurrentTimeMillis(millis);
		return DateUtil.format(date, format);
	}

	/**
	 * ����ʱ���ַ�
	 * @param dateString
	 * @param format
	 * @param locale
	 * @param timeZone
	 * @return
	 */
	public static Date parse(String dateString, String format, Locale locale,
			TimeZone timeZone) {
		SimpleDateFormat formatter = (SimpleDateFormat) DateFormat
				.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);
		formatter.applyPattern(format);
		formatter.setTimeZone(timeZone);
		Date date = null;
		try {
			date = formatter.parse(dateString);
		} catch (Exception e) {
			date = null;
		}
		return date;
	}

	/**
	 * ��õ�ǰʱ��ĸ�ʽ
	 * @param pattern
	 * @return 
	 */
	public static String getCurrentTime(String pattern) {
		SimpleDateFormat ilIlI11I1 = new SimpleDateFormat(pattern);
		return ilIlI11I1.format(new Date());
	}

	/**
	 * ͨ��ʱ����õ���ǰʱ��
	 * @param currentTimeMillis
	 * @return
	 */
	public static Date getDateByCurrentTimeMillis(long currentTimeMillis) {
		Timestamp now = new Timestamp(currentTimeMillis);
		return now;
	}

	/**
	 * �������ʱ��ļ������
	 * @param d1
	 * @param d2
	 * @return
	 */
	public static int getIntervalDays(Date begin, Date end) {

		if (begin == null || end == null)
			return 0;
		long between = (end.getTime() - begin.getTime()) / 1000;// ����1000��Ϊ��ת������
		return (int) between / (24 * 3600);
	}

	/**
	 * �������ʱ��ļ��Сʱ
	 * @param begin
	 * @param end
	 * @return
	 */
	public static int getIntervalHours(Date begin, Date end) {
		if (begin == null || end == null)
			return 0;
		long between = (end.getTime() - begin.getTime()) / 1000;
		return (int) between / 3600;
	}

	/**
	 * �������ʱ��ļ������
	 * @param begin
	 * @param end
	 * @return
	 */
	public static int getIntervalMinites(Date begin, Date end) {
		if (begin == null || end == null)
			return 0;
		long between = (end.getTime() - begin.getTime()) / 1000;
		return (int) between / 60;
	}

	/**
	 * �������ʱ��ļ����
	 * @param date
	 * @param date2
	 * @return
	 */
	public static int getIntervalSecond(Date begin, Date end) {

		if (begin == null || end == null)
			return 0;
		long between = (end.getTime() - begin.getTime()) / 1000;
		return (int) between;
	}

	/**
	 * ����кõ�ʱ����ʾ
	 * @param second2
	 * @return
	 */
	/*	public static String getFormatSecondText(int second2) {
			
			int hours = second2/(60*60);
			int minites = (second2%(60*60))/60;
			int seconds = (second2%60);
			StringBuilder sb = new StringBuilder();
			if(hours>0)sb.append(hours).append(I18n.getMessage("core.hours"));
			if(minites>0)sb.append(minites).append(I18n.getMessage("core.minites"));
			if(seconds>0)sb.append(seconds).append(I18n.getMessage("core.seconds"));
			return sb.toString();
		}*/

	/**
	 * ��õ�ǰ����1970�������
	 * @return
	 */
	public static int getCurrentTimeSeconds() {

		return (int) (System.currentTimeMillis() / 1000L);
	}

	/**
	 * ���SQLʱ��
	 * @param date
	 * @return
	 */
	public static java.sql.Date getSqlDate(Date date) {
		if (date == null)
			return null;
		return new java.sql.Date(date.getTime());
	}

	/**
	 * ���SQLʱ���
	 * @param date
	 * @return
	 */
	public static Timestamp getSqlTimestamp(Date date) {

		if (date == null)
			return null;
		return new java.sql.Timestamp(date.getTime());
	}

	/**
	 * ������ʱ���뵱ǰʱ����жԱȣ��Ƿ��������
	 * @param datetime
	 * @return
	 */
	public static String getTimeByDateStr(Date date) {
		String todySDF = "���� HH:mm";
		String yesterDaySDF = "���� HH:mm";
		String otherSDF = "MM��dd�� HH:mm";
		SimpleDateFormat sfd = null;
		String time = "";
		int flag = 0;
		try {
			Calendar dateCalendar = Calendar.getInstance();
			dateCalendar.setTime(date);
			Date now = new Date();
			Calendar targetCalendar = Calendar.getInstance();
			targetCalendar.setTime(now);
			targetCalendar.set(Calendar.HOUR_OF_DAY, 0);
			targetCalendar.set(Calendar.MINUTE, 0);
			if (dateCalendar.after(targetCalendar)) {
				sfd = new SimpleDateFormat(todySDF);
				time = sfd.format(date);
				flag = 1;
				// return time;
			} else {
				targetCalendar.add(Calendar.DATE, -1);
				if (dateCalendar.after(targetCalendar)) {
					System.out.println(yesterDaySDF);
					sfd = new SimpleDateFormat(yesterDaySDF);
					time = sfd.format(date);
					flag = 1;
					// return time;
				}
			}
			if (flag == 0) {
				sfd = new SimpleDateFormat(otherSDF);
				time = sfd.format(date);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		return time;
	}
	
	
	/*
	public static void main(String[] args) {
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm");
		String strDate = "2014-09-04 00:00";
		try {
			System.out.println(getTimeByDateStr(sdf.parse(strDate)));
		} catch (ParseException e) {
			e.printStackTrace();
		}
	}
	*/
	
}